import { Controller, Get, Post, Body, Patch, Param, Delete, Inject } from '@nestjs/common';
import { RbacUserService } from './rbac_user.service';
import { CreateRbacUserDto } from './dto/create-rbac_user.dto';
import { UpdateRbacUserDto } from './dto/update-rbac_user.dto';
import { UserLoginDto } from './dto/user-login.dto';
import { JwtService } from '@nestjs/jwt';

@Controller('rbac-user')
export class RbacUserController {
  constructor(private readonly rbacUserService: RbacUserService) {}

  @Inject(JwtService)
  private jwtService: JwtService

  @Get('init')
  async initData() {
    await this.rbacUserService.initData()
    return 'done'
  }

  @Post('loginByjwt')
  async loginByJwt(@Body() userLogin: UserLoginDto) {
    const user = await this.rbacUserService.loginByJwt(userLogin)
    console.log(user)
    const token = this.jwtService.sign({
      user: {
        username: user.username,
        roles: user.roles
      }
    })
    return {
      token
    }
  }

}
